
//配件点击事件
function	pj_click(obj,pj_id){
	var name	=	$(obj).attr('name');
	var current_class	=	$(obj).attr('class');
	if(current_class	==	'selected'){
		$(obj).attr('class','');
		$("#pj_id_"+pj_id).val(0);
	}else{
		$(obj).attr('class','selected');
		$("#pj_id_"+pj_id).val(pj_id);
	}
	var pj_ids	=	'',suffix='';
	$("input[name='pj_id[]']").each(function(){
			pj_ids	+=	suffix+$(this).val();
			suffix	=	',';
	})
	$("#pj_ids").val(pj_ids);
}

//属性点击事件
function	property_click(obj,suffix,property_id){
	var name	=	$(obj).attr('name');
	$("li[name='"+name+"']").attr('class','');
	$(obj).attr('class','selected');
	
	$("#property_"+suffix).val(property_id);
	//自动下一步
	auto_go_next(obj);
}
//描叙点击事件
function	item_click(obj,suffix,desc_id){
	var default_id	=	arguments[3] ? arguments[3] : desc_id;
	if(default_id	!=	desc_id){		//可多选时
		var name			=	$(obj).attr('name');
		var current_class	=	$(obj).attr('class');
		if(current_class	==	'selected'){
			$(obj).attr('class','');
			$("#desc_id_"+suffix).val(default_id);
			btn_step2_next_html();
			return;
		}
	}
	
	var name	=	$(obj).attr('name');
	$("li[name='"+name+"']").attr('class','');
	$(obj).attr('class','selected');
	//赋值描叙id
	$("#desc_id_"+suffix).val(desc_id);
	//子类id
	var	child_id	=	'pingu_mx_'+suffix+'_child';
	//删除现有子类内容
	$("#"+child_id).remove();
	
	if($("#pingu_mx_"+desc_id).length!=0){
		//加载子类描叙
		var html	=	"";
		html		+=	'<div id="'+child_id+'">';
		html		+=	'<dd class="" id="pingu_mx_'+desc_id+'">';
		html		+=	'<input type="hidden" name="desc_id[]" id="desc_id_'+desc_id+'" value="0" />';
		html		+=	$("#pingu_mx_"+desc_id).html();
		html		+=	'</dd>';
		html		+=	'</div>';
	}
	$("#pingu_mx_"+suffix).after(html);
	//自动下一步
	auto_go_next(obj);
	btn_step2_next_html();
}

function auto_go_next(obj){
	var p_div_id	=	$(obj).parent().parent().parent().parent().attr('id');
	if(p_div_id		==	'property_step1'){
		var n		=	true;
		$("#"+p_div_id+" input").each(function(){if($(this).val()==0){n=false;return false;}});
		if(n)	step1_next();
	}
}

function btn_step2_next_html(){
	var n	=	$("#property_step2").find(".selected").length;
	if(n	==	0){
		$("#btn_step2_next").html('都没问题，跳过');
	}else{
		$("#btn_step2_next").html('下一步');
	}
}

//提交价格
function sub_price(obj){
	var property_ids	=	'',psuffix	=	'';
	var desc_ids		=	'',dsuffix	=	'';
	var property_flag	=	true;
	$("input[name='property[]']").each(function(){
		if($(this).val()==0){
			property_flag	=	false;
			return;
		}
		property_ids	+=	psuffix+$(this).val();
		psuffix			=	',';
	})
	if( ! property_flag){
		alert("请选择评估参数");
		return false;
	}
	var desc_flag	=	true;
	$("input[name='desc_id[]']").each(function(){
		if($(this).val()==0){
			desc_flag	=	false;
			return false;
		}
		desc_ids	+=	dsuffix+$(this).val();
		dsuffix		=	',';
	})
	if( ! desc_flag){
		alert("请选择评估参数");
		return false;
	}
	
	if(desc_ids.length	==	0){
		alert("无评估参数");
		return false;
	}
	
	$(obj).attr('disabled','disabled');
	$("#myFrom").submit();
}

//加入购物车、官方回购、发布淘二手、寄售
function show_price_action(request_url,act,gourl){
	//防止频繁操作
	if($("#if_show_price")	==	0)	return false;
	
	var ifadd	=	$("#ifadd").val();
	var post_arr	=  new Array();
	post_arr["sn"]	=	$.trim($("#sn").val());
	post_arr["act"]	=	act;
	var js_arr	=	new Array();
	js_arr['ifadd']	=	js_arr;
	js_arr['act']	=	act;
	js_arr['gourl']	=	gourl;
	
	if(act == 'addcart'){
		ajax_post(request_url,post_arr,goods_action,js_arr);	
	}else{
		if(ifadd > 0){
			location.href	=	gourl;
		}else{
			ajax_post(request_url,post_arr,goods_action,js_arr);	
		}
	}
}
function goods_action(data,js_arr){
	$("#if_show_price").val(1);	//解锁
	var obj	=	eval(data);
	
	if(obj[0]	==	1){
		$("#ifadd").val(1);
		$("#cart_total_items").attr('class','mui-mbar-wz-em');
		addcart_result(obj);
		if(js_arr['act'] == 'addcart')	alert("已加入二货箱");
		else	location.href	=	js_arr['gourl'];
	}else{
		alert("数据传输有误");	
	}
}

/**
*	操作成功 对返回结果进行处理
**/
function addcart_result(obj){
	var rowid	=	obj[2]['rowid'];
	var id	=	obj[2]['id'];
	var price	=	obj[2]['price'];
	var name	=	obj[2]['name'];
	var img	=	obj[2]['img'];
	var remove_url	=	obj[2]['remove_url'];
	var base_url	=	obj[2]['base_url'];
	var sn	=	obj[2]['sn'];
	var goods_url	=	obj[2]['goods_url'];
	var tr	=	'';
	tr	+=	'<tr id="cart_items_'+rowid+'" onmouseover="cart_tr_mouseover(this)" onmouseout="cart_tr_mouseout(this)">';
	tr	+=	'<td class="table-padding">';
	tr	+=	'<div class="mui-pic">';
	tr	+=	'<label>';
	tr	+=	'<input type="checkbox" value="'+rowid+'" name="rowid[]" id="rowid_'+rowid+'" onclick="one_price_click()" />';
	tr	+=	'</label>';
	tr	+=	'<a href="'+goods_url+'" target="_blank">';
	tr	+=	'<img src="http://image.mitaole.com/goods_a/'+img+'" width="74" height="56" />';
	tr	+=	'</a></div></td>';
	tr	+=	'</td>';
	tr	+=	'<td><p>';
	tr	+=	'<a href="'+goods_url+'" target="_blank">'+name+'</a>';
	tr	+=	'</p></td>';
	tr	+=	'<td><em>￥ '+price+'</em>';
	tr	+=	'<input type="hidden" name="cart_goods_price[]" id="cart_goods_price_'+rowid+'" value="'+price+'" />';
	tr	+=	'<input type="hidden" name="cache_sn[]" id="cache_sn_'+rowid+'" value="'+sn+'" />';
	tr	+=	'<input type="hidden" name="cache_gid[]" id="cache_gid_'+rowid+'" value="'+id+'" />';
	tr	+=	'<div class="table-close"><a href="javascript:void(0)" onclick="remove_cart_extends(\''+remove_url+'\',\''+rowid+'\')"><img src="'+base_url+'images/table_close.png" /></a></div>';
	tr	+=	'</td></tr>';
	$("#cart_goods_list").append(tr);
	//数量
	$("#cart_total_items").html(obj[1]['cart_total_items']);
}

//查看价格
function show_price(url,act,func,elem_id){
	var property_ids	=	'',psuffix	=	'';
	var desc_ids		=	'',dsuffix	=	'';
	var property_flag	=	true;
	$("input[name='property[]']").each(function(){
		if($(this).val()==0){
			property_flag	=	false;
			return;
		}
		property_ids	+=	psuffix+$(this).val();
		psuffix			=	',';
	})
	if( ! property_flag){
		alert("请选择评估参数");
		return false;
	}
	var desc_flag	=	true;
	$("input[name='desc_id[]']").each(function(){
		if($(this).val()==0){
			desc_flag	=	false;
			//alert($(this).attr('id'));
			return false;
		}
		desc_ids	+=	dsuffix+$(this).val();
		dsuffix		=	',';
	})
	if( ! desc_flag){
		alert("请选择评估参数");
		return false;
	}
	
	if(desc_ids.length	==	0){
		alert("无评估参数");
		return false;
	}
	func			=	typeof(func)=='undefined'?'add':func;
	var gid			=	$("#gid").val();
	var package_id	=	$("#package_id").val();
	var pj_ids		=	$("#pj_ids").val();
	var shop_id		=	$("#shop_id").val();
	//防止频繁操作
	if($("#if_show_price")	==	0)	return false;
	$("#if_show_price").val(0);		//加锁
	if(act	==	'show_price'){		//查看价格
		var view	=	$("#view").val();	//视图文件
		if(view	==	'goods_info')	$("#loading_div").css('display','block');
		$.post(url,{'gid':gid,'package_id':package_id,'property_ids':property_ids,'desc_ids':desc_ids},function(data){
			//alert(data);
			show_price_new();
			$("#if_show_price").val(1);	//解锁
			var obj	=	eval(data);
			var add_html	=	'';
			if(obj[0]	==	1){
				var arr	= obj[1];	//默认显示值
				$("#shop_id").val(arr['shop_id']);	//回购供应商
				var pg_price	=	arr['pg_price'];
				$("#huigo_pg_price").html(''+pg_price+' ');
				var flag_html	=	'';
				var h_price		=	obj[2]['pg_price']+'元';
				if(obj[1]['shop_id']==obj[2]['shop_id']){	//官方报价最高时
					flag_html	=	'<span class="gao">出价最高</span>';
					h_price		=	"<strong>"+obj[2]['pg_price']+"元</strong>";
				}
				add_html	+=	'<dd><span class="biao1"></span><span class="name">米淘乐官方报价</span><span class="price"> '+h_price+'</span>'+flag_html+'<div class="clear"></div></dd>';
				
				for(var i=0;i<obj[3].length;i++){
					var ll	=	obj[3][i];
					flag_html	=	'';
					h_price		=	ll['shop_price']+'元';
					if(obj[1]['shop_id']==ll['shop_id']){
						flag_html	=	'<span class="gao">出价最高</span>';
						h_price		=	"<strong>"+ll['shop_price']+"元</strong>";
					}
					add_html+=	'<dd><span class="biao2"></span><span class="name"><i>[认证商家]</i>'+ll['shop_name']+'<ins class="ahs_img1" title="工商注册认证">&nbsp;</ins><ins class="ahs_img2" title="已缴纳全额保证金">&nbsp;</ins></span><span class="price">'+h_price+'</span>'+flag_html+'<div class="clear"></div></dd>';
				}
				$("#dl_show_shop dd").remove();
				$("#dl_show_shop").append(add_html);
				
			}else{
				alert("数据传输有误");
			}
		});
		if(view	==	'goods_info'){
			$("#tijiao_div").css('display','block');
			$("#loading_div").css('display','none');
		}
	}else if(act	==	'addcart'){	//加入购物车
		if(func	==	'sub')	$("#btn_sub_cart").attr('disabled','disabled');
		var ifjia	=	$("#ifjia").val();
		$.post(url,{'gid':gid,'package_id':package_id,'property_ids':property_ids,'desc_ids':desc_ids,'pj_ids':pj_ids,'func':func,'ifjia':ifjia,'shop_id':shop_id},function(data){																															
			var obj	=	eval(data);
			//提交回购单操作
			if(func	==	'sub'){		
				if(obj[0]	==	1){
					gourl			=	url.replace("ajax/addcart","order/cart");;
					location.href	=	gourl;
				}else{
					div_id		=	arguments[3]?arguments[3]:'btn_sub_cart';
					$("#"+div_id).attr('disabled',false);
					alert("数据传输有误");
				}
				return;
			}
			
			//添加购物车操作
			if(obj[0]	==	1){	
				//购物车总信息
				var arr	= obj[1];
				$("#cart_total_items").html(arr['cart_total_items']);
				$("#cart_total").html('￥'+arr['cart_total']);
				
				//添加商品信息
				var item_info	= obj[2];
				var item_class	=	arr['cart_total_items']%2==0?'':'class="on"';
				var item_id		=	'cart_items_'+item_info['rowid'];
				var html=	'';
				html	+=	'<dd '+item_class+' id="'+item_id+'" onmousemove="car_dd_mousemove(this)" onmouseout="car_dd_onmouseout(this)">';
				html	+=	'<div class="img2"><img src="'+item_info['img']+'"  width="50" height="38"/></div>';
			  	html	+=	'<div class="name">'+item_info['name']+'</div>';
				html	+=	'<div class="price">回购价 <em>'+item_info['price']+'</em> 元</div>';
				html	+=	'<a href="javascript:remove_cart(\''+item_info['remove_url']+'\',\''+item_info['rowid']+'\')" class="close_btn"><span class="car_colse_img">&nbsp;</span></a>';
				html	+=	'<div class="clear"></div>';
				html	+=	'</dd>';
				$("#cart_heji").before(html);
				
				//加入购物车后初始我要加价
				$("#jia_price_div").attr('class','ex_btn');
				$("#ifjia").val("n");
				alert("已加入回购车");
			}else if(obj[0]	==	2){
				alert("数据传输有误");
			}else if(obj[0]	==	0){
				alert("操作失败");
			}
		});
	}
}

function remove_cart(url,rowid){
	$.post(url,{'rowid':rowid},function(data){
		var obj	=	eval(data);
		if(obj[0]	==	1){
			var arr	= obj[1];
			$("#cart_items_"+rowid).hide("slow"); 
			$("#cart_total_items").html(arr['cart_total_items']);
			$("#cart_total").html('￥'+arr['cart_total']);
			
		}else if(obj[0]	==	0){
			alert("操作失败");
		}
	});
}

function	mx_mousemove(obj){
	$(obj).find('.samp').show();
}
function	mx_onmouseout(obj){
	$(obj).find('.samp').hide();
}

function	car_dd_mousemove(obj){
	$(obj).children('a').children('span').attr('class','car_colse_img2');
}
function	car_dd_onmouseout(obj){
	$(obj).children('a').children('span').attr('class','car_colse_img');
}

function window_scroll(){
	if($(window).scrollTop()>647){
		$(".bar1").css({"position":"fixed","z-index":77,"top":0,"margin-top":"0px"});
		$(".car").css({"position":"fixed","z-index":77,"top":0,"margin-top":"0px"})
	}else{
		$(".bar1").removeAttr("style");
		$(".car").removeAttr("style");
	}	
}

//显示评论
function show_goods_review(src){
	if($("#goods_review").attr('src')	==	''){
		$("#goods_review").attr('src',src);
	}
}

//显示回购承诺
function show_goods_chengnuo(src){
	if($("#goods_chengnuo").attr('src')	==	''){
		$("#goods_chengnuo").attr('src',src);
	}
}

//显示了解我们如何质检
function show_goods_zhijian(src){
	if($("#goods_zhijian").attr('src')	==	''){
		$("#goods_zhijian").attr('src',src);
	}
}

//添加提交操作
function check_from(obj,action){
	var img_length		=	$(".First_list").children('dd').length;
	if(img_length == 0){
		alert("请先上传宝贝照片");
		return false;
	}
	var property_ids	=	'',psuffix	=	'';
	var desc_ids		=	'',dsuffix	=	'';
	
	var property_flag	=	true;
	$("input[name='property[]']").each(function(){
		if($(this).val()==0){
			property_flag	=	false;
			return;
		}
		property_ids	+=	psuffix+$(this).val();
		psuffix			=	',';
	})
	if( ! property_flag){
		alert("请选择属性");
		return false;
	}
	
	var desc_flag	=	true;
	$("input[name='desc_id[]']").each(function(){
		if($(this).val()==0){
			desc_flag	=	false;
			return;
		}
		desc_ids	+=	dsuffix+$(this).val();
		dsuffix		=	',';
	})
	if( ! desc_flag){
		alert("请选择评估参数");
		return false;
	}
	
	if(desc_ids.length	==	0){
		alert("无评估参数");
		return false;
	}
	
	var jiaoyi_method_check	=	$("input[name='jiaoyi_method[]']:checked").length;
	if(jiaoyi_method_check	==	0){
		alert('请选择支付方式');
		return false;
	}
	/*
	var jiaoyi_method	=	$("#jiaoyi_method_2").attr('checked');
	if(jiaoyi_method){
		var	ifzhijian_check	=	$("input[name='ifzhijian']:checked").length;
		if(ifzhijian_check	==	0){
			alert('请选择质检费用承担者');
			return false;
		}
	}*/
	
	if($("#province").val() == 0){
		alert("请选择省份");
		$("#province").focus();
		return false;
	}
	
	if($("#city").val() == 0){
		alert("请选择城市");
		$("#city").focus();
		return false;
	}
	
	if($("#district").val() == 0){
		alert("请选择区县");
		$("#district").focus();
		return false;
	}
	
	/*
	var ifbaoyou_check	=	$("input[name='ifbaoyou']:checked").length;
	if(ifbaoyou_check	==	0){
		alert('请选择运费说明');
		return false;
	}
	
	var ifbaoyou_val	=	$("input[name='ifbaoyou']:checked").val();
	var express_amount	=	Number($("#express_amount").val());
	if(isNaN(express_amount)){
		alert("快递费用格式有误");
		$("#express_amount").focus();
		return false;
	}
	if(ifbaoyou_val	==	'n'	&&	express_amount==0){
		alert("请输入快递费用");
		$("#express_amount").focus();
		return false;
	}
	
	if($.trim($("#stime").val()) == ''	||	$.trim($("#etime").val()) == ''){
		alert("请输入竞拍时间");
		return false;
	}
	*/
	
	var jp_days	=	Number($("#jp_days").val());
	if( ! isNaN(jp_days)){
		if(jp_days<1 || jp_days > 7){
			alert("竞拍时间有效天数1到7天");
			$("#jp_days").focus();
			return false;
		}
	}else{
		alert("竞拍时间请输入数字");
		$("#jp_days").focus();
		return false;
	}
	
	
	if($("#aprice").val()==0){
		alert("请选择加价幅度");
		$("#aprice").focus();
		return false;
	}
	
	if($.trim($("#keywords").val())==''){
		alert("请填写宝贝标题");
		$("#keywords").focus();
		return false;
	}
	var this_id	=	$(obj).attr('id');
	if(this_id	==	'btn_submit_2')	$("#is_audit").val(2);
	
	$("#sub_button").attr('disabled','disabled');	//防止网络延迟重复提交
	$("#sub_from").attr('action',action);
	$("#sub_from").submit();
}

//删除会员商品图
function del_mgid_img(id,gourl){
	var post_arr	=  new Array();
	post_arr["id"]	=	id;
	
	var js_arr		=	new Array();
	js_arr['id']	=	id;
	ajax_post(gourl,post_arr,return_del_mgid_img,js_arr);
}
function return_del_mgid_img(data,js_arr){
	var id	=	js_arr['id'];
	if(data == '1'){
		$("#mgid_"+id).hide("slow");
	}
}


/*新评估start*/
function step1_next(){
	var flag	=	true;
	$("#property_step1 input").each(function(){if($(this).val()==0){flag=false;return false;}});
	if(! flag){
		alert('请选择评估参数');
		return false;
	}
	
	$("#property_step2").show();
	$("#property_step1").hide();
	$("#speed2").show();
	$("#speed1").hide();
}

function step1_next_one(){
	/*var flag	=	true;
	$("#property_step1 input").each(function(){if($(this).val()==0){flag=false;return false;}});
	if( false){
		alert('请选择评估参数');
		return false;
	}*/
	
	$("#property_step2").show();
	$("#property_step1").hide();
	$("#speed2").show();
	$("#speed1").hide();
}
function step2_next(){
   $("#property_step3").show();
   $("#property_step2").hide();
   $("#speed3").show();
   $("#speed2").hide();
}
function step2_back(){
   $("#property_step1").show();
   $("#property_step2").hide();
   $("#speed1").show();
   $("#speed2").hide();
}
function step3_back(){
   $("#property_step2").show();
   $("#property_step3").hide();
   $("#speed2").show();
   $("#speed3").hide();
}
/*新评估end*/